package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"main/api/request"
	"main/internal/dao"
	"main/internal/model"
	"main/pkg"
	"net/http"
	"path/filepath"
	"strconv"
)

func AddArticle(c *gin.Context) {
	var req request.AddArticleReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	article := model.Article{
		UserId:  c.GetUint("userId"),
		Title:   req.Title,
		Content: req.Content,
		CType:   req.CType,
		FileUrl: req.FileUrl,
	}
	if !dao.Create(&article) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": article,
	})
}

func BatchArticle(c *gin.Context) {
	var req []request.AddArticleReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	for _, article := range req {
		each := model.Article{
			UserId:  c.GetUint("userId"),
			Title:   article.Title,
			Content: article.Content,
			CType:   article.CType,
			FileUrl: article.FileUrl,
		}
		if !dao.Create(&each) {
			c.JSON(http.StatusOK, gin.H{
				"code": 500,
				"msg":  "失败",
				"data": nil,
			})
			return
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": req,
	})
}

func UpdateArticle(c *gin.Context) {
	var req request.UpdateArticleReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	article := dao.GetArticleById(req.Id)
	if article.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "改文章不存在",
			"data": nil,
		})
		return
	}
	if article.UserId != c.GetUint("userId") {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "当前用户没有权限更改",
			"data": nil,
		})
		return
	}
	updateData := model.Article{
		Id:      req.Id,
		Title:   req.Title,
		Content: req.Content,
		CType:   req.CType,
		FileUrl: req.FileUrl,
	}
	if !dao.Update(&updateData) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "更新失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": article,
	})
}

func UploadArticle(c *gin.Context) {
	file, _ := c.FormFile("file")

	uuid := uuid.NewString()
	//配置文件里面
	filename := uuid + filepath.Ext(file.Filename)
	dst := "./static/upload/" + filename

	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	pkg.UploadOss(dst, file.Filename)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": pkg.GetOssUrl(file.Filename),
	})

}

func RemoveArticle(c *gin.Context) {
	var req request.RemoveArticleReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	result := dao.Delete(&model.Article{Id: req.Id})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": result,
	})
}

func DetailArticle(c *gin.Context) {
	idint, _ := strconv.Atoi(c.Query("id"))
	id := uint(idint)

	var result model.Article
	dao.Query(id, &result)
	if result.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "文章不存在",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": result,
	})
}
